/**
 * ...
 * @author Axel Huizinga - axel@cunity.me
 */

package;

import flash.Boot;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.Lib;
import flash.system.ApplicationDomain;
import flash.xml.XML;
import haxe.ds.IntMap.IntMap;
import haxe.ds.StringMap;
import haxe.Log;
import haxe.CallStack;
import me.cunity.animation.FastTween;
import me.cunity.animation.sandy.STween;
//import me.cunity.animation.ScreenPlay;
import me.cunity.animation.TimeLine;
import me.cunity.debug.Out;
import me.cunity.layout.Alignment;
import me.cunity.tools.sandy.BitmapResource;
import me.cunity.ui.mask.Stripes;
import sandy.materials.Appearance;
import sandy.materials.BitmapMaterial;
import sandy.core.Scene3D;
import sandy.core.scenegraph.Camera3D;
import sandy.core.scenegraph.Group;

class Main  extends MovieClip
//class Main extends Sprite, implements ScreenPlay
{

	public static var spaceLib:MovieClip;
	public var globe:Planet;
	public var nextCall:Dynamic;
	public var storyBoard:StringMap<Dynamic>;
	public var timeLine(default, null):TimeLine;
	public var resizing:Bool;
	var world:Scene3D;
	
	//static var field:IntMap = new IntMap();
	
	public static function main() 
	{
		Out.traceTarget = DebugOutput.CONSOLE;
		//Out.traceTarget = DebugOutput.NATIVE;
		Log.trace = Out._trace;		
		trace('hello');
		var m = new Main();

	}
	
	public function new()
	{
		Out.traceTarget = DebugOutput.CONSOLE;
		//Out.traceTarget = DebugOutput.NATIVE;
		Log.trace = Out._trace;
		//spaceLib = Lib.attach('SpaceLib');
		name = 'GlobeOne';
		Lib.current.assets.set(name, this);
		trace(name+ ' hello: ' + Lib.current.name + ':' );
		//trace(name+ ' hello: ' + Lib.current.name + ':' + field);
		//Out.dumpStack(Stack.callStack());
		super();
		storyBoard  = new StringMap();
	}
	
	function onAdded2Stage(evt:Event)
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAdded2Stage, false);
		init(400, 320);
	}
	
	public function init(w:Float = 400, h:Float = 320, location:Dynamic = null)
	{
		//Lib.current.addChild(this);
		trace(this + ':' + Lib.current.name + ':' + parent.name);
		trace(Lib.current.name + ' == ' + Lib.current.parent.name + ':'  + Std.string(Lib.current.parent.stage == Lib.current.stage));
		//return;
		var bBox: Rectangle = new Rectangle(0, 0, w, h);
		if (!resizing)
		{
			globe = new Planet(this);
			//addChild(globe);
			trace(Std.string(location));
			world = createWorld(this, bBox);
			globe.init(world, location);
			//space.setBackground(new Appearance(new  BitmapMaterial(new BitmapResource('Sky').drawScaledData(bBox, Alignment.TOP))), bBox);
			var bData:BitmapData = new BitmapResource('Sky').bmp.bitmapData;
			trace(bData.rect.toString());
			
			globe.setBackground(w, h, new Appearance(new  BitmapMaterial(bData)));		
			globe.draw();
		}
		else
		{
			globe.setBackground(w, h);
		}
		
		
		//tL.add(globe.setGlow);
		//tL.add(globe.planetAction);
		//globe.planetAction(tL);
		trace (Lib.current.loaderInfo.loaderURL);
		if (false && Lib.current.stage != null)
		{
			trace('adding to stage');
			//Lib.current.addChild(this);
			timeLine = new TimeLine(null, new XML('<TimeLine  storyBoard="go,rainbow" />'));
			timeLine.addTween(null, new XML('<STween start="500"  id="go" />'), globe.action);
			timeLine.addTween(null, new XML('<STween start="0"  id="rainbow" />'), globe.rainbow);
			//globe.action();
			timeLine.next();
			untyped nextCall = timeLine.next();
		}
		
		else
		{
			storyBoard.set('go', globe.action);
			storyBoard.set('rainbow', globe.rainbow);
		}
	}
	
	/*static*/ 
	function createWorld(clip:Sprite = null, box:Rectangle = null):Scene3D
	{
		if (box == null)
		{
			//box = new Rectangle(stage.getBounds(this));
			box = Lib.current.stage.getBounds(Lib.current);
		}
		
		if (clip == null)
			clip = Lib.current;
		trace(box.toString());
		var cam:Camera3D = createCamera(box);
		var world:Scene3D = new Scene3D ("scene", clip, cam, new Group("root"));

		world.root.addChild(cam);
		world.rectClipping = true;
		return world;
	}
	
	function createCamera(box:Rectangle):Camera3D
	{
		var cam:Camera3D = new Camera3D(Std.int(box.width), Std.int(box.height));
		cam.x = cam.y = 0;
		cam.z = -1000;		
		return cam;
	}
	
	public function reset()
	{
		FastTween.removeAllTweens();
		STween.removeAllTweens();
		storyBoard  = new StringMap<Dynamic>();
	}
	
	public function resize(w:Float , h:Float)
	{		
		resizing = true;
		reset();
		world.camera = createCamera(new Rectangle(0, 0 , w, h));
		trace('world.camera.viewport.width:' + world.camera.viewport.width);
		//world.camera.viewport.width = Std.int(w);
		//world.camera.viewport.height = Std.int(h);
		init(w, h);
		globe.action();
		trace('ok');
	}
	
	public function destroy()
	{
		FastTween.removeAllTweens();
		globe.destroy();
		globe = null;
	}
}